//
//  ProductDetailsPageViewController.m
//  LocalGoods2
//
//  Created by Feihong Ye on 2018/11/6.
//  Copyright © 2018年 Xiaodong Huang. All rights reserved.
//

#import "ProductDetailsPageViewController.h"
#import "Header.h"
#import "ProductHeaderImageCell.h"

#import <SDCycleScrollView.h>

static NSString *productHeaderImageCell_id = @"productHeaderImageCell_id";
@interface ProductDetailsPageViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) UIView *headerView;

@property (nonatomic, strong) UICollectionView *collectionView;
@end

@implementation ProductDetailsPageViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [super havePopVc];
    
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage = [UIImage new];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
    _tableView.backgroundColor = [UIColor colorFromRed:245 green:245 bule:245];
    [self.view addSubview:_tableView];
    
    self.headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREAM_WEIGHT, 667/2)];
    _headerView.backgroundColor = [UIColor whiteColor];
     [self setUpUI];
    _tableView.tableHeaderView = _headerView;
    
    self.extendedLayoutIncludesOpaqueBars = YES;
    
    if (@available(iOS 11.0, *)) {
        
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        
    } else {
        
        self.automaticallyAdjustsScrollViewInsets = NO;
        
    }
    
}

- (void)setUpUI {
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.minimumLineSpacing = 0;
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.collectionView = [[UICollectionView alloc] initWithFrame:_headerView.bounds collectionViewLayout:flowLayout];
    _collectionView.backgroundColor = [UIColor whiteColor];
    flowLayout.itemSize = _collectionView.bounds.size;
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    _collectionView.showsVerticalScrollIndicator = NO;
    _collectionView.showsHorizontalScrollIndicator = NO;
    _collectionView.pagingEnabled = YES;
    [_headerView addSubview:_collectionView];
    
    [_collectionView registerClass:[ProductHeaderImageCell class] forCellWithReuseIdentifier:productHeaderImageCell_id];
    
}

// MARK: - UICollectionViewDelegate, UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 3;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    ProductHeaderImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:productHeaderImageCell_id forIndexPath:indexPath];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    ProductHeaderImageCell *cell = (ProductHeaderImageCell *)[collectionView cellForItemAtIndexPath:indexPath];
    
    
    CGRect cellRect = [collectionView convertRect:cell.frame toView:collectionView];
    CGRect rect2 = [collectionView convertRect:cellRect toView:self.view];
    
    
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    
    UIView *bgView = [[UIView alloc] initWithFrame:window.bounds];
    bgView.backgroundColor = [UIColor blackColor];
    bgView.alpha = 0;
    [window addSubview:bgView];
    
//    UIImageView *gDImageView = [[UIImageView alloc] initWithImage:image(@"YUSHI3")];
//    gDImageView.backgroundColor = [UIColor clearColor];
//    gDImageView.frame = rect2;
//    [window addSubview:gDImageView];
    
    SDCycleScrollView *cyckeScrollView = [SDCycleScrollView cycleScrollViewWithFrame:rect2 delegate:nil placeholderImage:image(@"banner加载失败与加载中")];
    cyckeScrollView.backgroundColor = [UIColor whiteColor];
    cyckeScrollView.localizationImageNamesGroup = @[@"YUSHI3", @"YUSHI1", @"YUSHI2"];
    cyckeScrollView.autoScroll = NO;
    cyckeScrollView.showPageControl = NO;
    [window addSubview:cyckeScrollView];
    
    
    [UIView animateWithDuration:1 animations:^{
        cyckeScrollView.center = self.view.center;
        bgView.alpha = 1;
    } completion:^(BOOL finished) {
        
    }];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
